package com.wyzz.hr.action;

import java.util.*;
import org.hibernate.FetchMode;
import org.hibernate.criterion.DetachedCriteria;

import com.wyzz.hr.base.BaseAction;
import com.wyzz.hr.base.Status;
import com.wyzz.hr.bo.ICompaplanBo;
import com.wyzz.hr.pojo.Employee;
import com.wyzz.hr.pojo.Empsalaryadj;
import com.wyzz.hr.pojo.Empsalaryconfig;
import com.wyzz.hr.pojo.Emptype;
import com.wyzz.hr.pojo.Location;
import com.wyzz.hr.spring.SpringBeanFactory;
import com.wyzz.hr.util.BaseCrit;
import com.wyzz.hr.util.Pager;

public class SearchBatchCompaplan extends BaseAction implements Status {

	public SearchBatchCompaplan() {
		emp = new Employee();
		empSalaryAdj = new Empsalaryadj();
		page = new Pager();
	}

	public String execute() throws Exception {
		DetachedCriteria dc = searchConfWithEmp_DC();
		addCriteria(dc);
		getDrillDownList();
		yearAndMonth = getCalendarYearAndMonth();
		curEffDate = (String) yearAndMonth.get(2);
		ICompaplanBo compaplanBo = (ICompaplanBo) SpringBeanFactory
				.getBean("compaplanBo");
		salaryconfList = compaplanBo.mysearchBatchCompaplan(dc, page);
		compaplanBo.calcSalaryConfWithComp(salaryconfList);
		return "success";
	}

	private DetachedCriteria searchConfWithEmp_DC() {
		DetachedCriteria dc = DetachedCriteria.forClass(Empsalaryconfig.class);
		dc.setFetchMode("employee", FetchMode.JOIN);
		dc.setFetchMode("employee.empType", FetchMode.JOIN);
		dc.setFetchMode("employee.empDeptNo", FetchMode.JOIN);
		dc.setFetchMode("employee.empLocationNo", FetchMode.JOIN);
		dc.setFetchMode("escEsavId", FetchMode.JOIN);
		dc.setFetchMode("escJobgrade", FetchMode.JOIN);
		dc.createAlias("employee", "emp");
		return dc;
	}

	private void addCriteria(DetachedCriteria dc) {
		BaseCrit.addEmpDC(dc, "emp", emp.getEmpName());
		BaseCrit.addDeptDC(dc, (new StringBuilder()).append("emp.").append(
				Employee.PROP_EMP_DEPT_NO).toString(), (new StringBuilder())
				.append("emp.").append(Employee.PROP_EMP_PB_NO).toString(),
				Integer.valueOf(1), emp.getEmpDeptNo());
		BaseCrit.addDC(dc, (new StringBuilder()).append("emp.").append(
				Employee.PROP_EMP_TYPE).toString(), Emptype.PROP_ID,
				new Object[] { emp.getEmpType() });
		BaseCrit.addDC(dc, (new StringBuilder()).append("emp.").append(
				Employee.PROP_EMP_LOCATION_NO).toString(), Location.PROP_ID,
				new Object[] { emp.getEmpLocationNo() });
		BaseCrit.addDC(dc, (new StringBuilder()).append("emp.").append(
				Employee.PROP_EMP_STATUS).toString(), "eq",
				new Integer[] { Integer.valueOf(1) });
	}

	private void getDrillDownList() {
		departmentList = getDrillDown("Department", new String[0]);
		location = getDrillDown("Location", new String[0]);
		empType = getDrillDown("EmpType", new String[0]);
		jobgradePro = getDrillDown("Jobgrade", new String[0]);
		esaList = getDrillDown("EmpSalaryAcct", new String[0]);
		ecptypeList = getDrillDown("EcpType", new String[0]);
	}

	private List getCalendarYearAndMonth() {
		List yearAndMonth = new ArrayList();
		Calendar calendar = Calendar.getInstance();
		int startYear = calendar.get(1);
		int startMonth = calendar.get(2);
		yearAndMonth.add((new StringBuilder()).append(startYear).append("-")
				.append(
						startMonth <= 9 ? ((Object) ((new StringBuilder())
								.append("0").append(startMonth).toString()))
								: ((Object) (Integer.valueOf(startMonth))))
				.toString());
		for (int i = 0; i < 13; i++) {
			int currentYear = startYear;
			int currentMonth = startMonth + i;
			calendar.set(currentYear, currentMonth, 1);
			currentYear = calendar.get(1);
			currentMonth = calendar.get(2) + 1;
			if (currentMonth < 10)
				yearAndMonth.add((new StringBuilder()).append(currentYear)
						.append("-0").append(currentMonth).toString());
			else
				yearAndMonth.add((new StringBuilder()).append(currentYear)
						.append("-").append(currentMonth).toString());
		}

		return yearAndMonth;
	}

	public Pager getPage() {
		return page;
	}

	public void setPage(Pager page) {
		this.page = page;
	}

	public List getDepartmentList() {
		return departmentList;
	}

	public void setDepartmentList(List departmentList) {
		this.departmentList = departmentList;
	}

	public List getEcptypeList() {
		return ecptypeList;
	}

	public void setEcptypeList(List ecptypeList) {
		this.ecptypeList = ecptypeList;
	}

	public List getJobgradePro() {
		return jobgradePro;
	}

	public void setJobgradePro(List jobgradePro) {
		this.jobgradePro = jobgradePro;
	}

	public List getLocation() {
		return location;
	}

	public void setLocation(List location) {
		this.location = location;
	}

	public List getEsaList() {
		return esaList;
	}

	public void setEsaList(List esaList) {
		this.esaList = esaList;
	}

	public Employee getEmp() {
		return emp;
	}

	public void setEmp(Employee emp) {
		this.emp = emp;
	}

	public List getManagers() {
		return managers;
	}

	public void setManagers(List managers) {
		this.managers = managers;
	}

	public Empsalaryadj getEmpSalaryAdj() {
		return empSalaryAdj;
	}

	public void setEmpSalaryAdj(Empsalaryadj empSalaryAdj) {
		this.empSalaryAdj = empSalaryAdj;
	}

	public List getSalaryconfList() {
		return salaryconfList;
	}

	public void setSalaryconfList(List salaryconfList) {
		this.salaryconfList = salaryconfList;
	}

	public List getEmpStatus() {
		return empStatus;
	}

	public void setEmpStatus(List empStatus) {
		this.empStatus = empStatus;
	}

	public List getEmpType() {
		return empType;
	}

	public void setEmpType(List empType) {
		this.empType = empType;
	}

	public List getYearAndMonth() {
		return yearAndMonth;
	}

	public void setYearAndMonth(List yearAndMonth) {
		this.yearAndMonth = yearAndMonth;
	}

	public String getCurEffDate() {
		return curEffDate;
	}

	public void setCurEffDate(String curEffDate) {
		this.curEffDate = curEffDate;
	}

	private static final long serialVersionUID = 0xc53a77b0ae03eebfL;
	private Employee emp;
	private Empsalaryadj empSalaryAdj;
	private List salaryconfList;
	private List jobgradePro;
	private List departmentList;
	private List ecptypeList;
	private List managers;
	private List location;
	private List esaList;
	private Pager page;
	private List empStatus;
	private List empType;
	private List yearAndMonth;
	private String curEffDate;
}
